app.controller("reportRecordCtrl", ["$scope",'datepickerPopupConfig', 'datepickerConfig','ReportApi','$location','sweetAlertUtil','$state',
    function($scope,datepickerPopupConfig, datepickerConfig,ReportApi,$location,sweetAlertUtil,$state) {
        $scope.code = $location.search().code;
        $scope.VM = {
            start_date:"",
            start_dates:"",
            is_records:"Y",
            is_aloof_pollute:"Y",
            is_well_clean:"Y",
            is_special_manager:"Y",
            is_health_cooks:"Y",
            is_poisonous_things:"Y",
            is_fresh_foods:"Y",
            is_enough:"Y",
            foods_condition:"Y",
            store_condition:"Y",
            is_pull_right:"Y",
            hold_person:$location.search().user_name,
            undertake_person:$location.search().cook_name,
            check_address:$location.search().address
        }
        $scope.startDate = ''
        $scope.startDates = ''


        //------------------------------start 初始化选择时间组件 --------------------
        initDatePicker()
        function initDatePicker() {
            datepickerConfig.minMode = "day"
            datepickerConfig.datepickerMode="day"
            datepickerConfig.showWeeks = false;
            datepickerPopupConfig.showButtonBar = false

            Date.prototype.format = function(format) {
                var o = {
                    "M+": this.getMonth() + 1, //month 
                    "d+": this.getDate(), //day 
                    "h+": this.getHours(), //hour 
                    "m+": this.getMinutes(), //minute 
                    "s+": this.getSeconds(), //second 
                    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
                    "S": this.getMilliseconds() //millisecond 
                }

                if (/(y+)/.test(format)) {
                    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                }

                for (var k in o) {
                    if (new RegExp("(" + k + ")").test(format)) {
                        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                    }
                }
                return format;
            }
        }
        //------------------------------end 初始化选择时间组件 --------------------


        $scope.submit = function(){
            $scope.startDate = $scope.VM.start_date!='' ? $scope.VM.start_date.format("yyyy-MM-dd") : ''
            $scope.startDates = $scope.VM.start_dates!='' ? $scope.VM.start_dates.format("yyyy-MM-dd") : '';
            if($scope.startDate == ''){
                sweetAlertUtil.fail("举办时间必须填写")
                return
            }
            if($scope.VM.manager_name=='' || $scope.VM.manager_name==undefined){
                sweetAlertUtil.fail("是否专人管理?必须填写姓名")
                return
            }
            if($scope.VM.manager_phone=='' || $scope.VM.manager_phone==undefined){
                sweetAlertUtil.fail("是否专人管理?必须填写电话")
                return
            }
            if($scope.VM.check_persons=='' || $scope.VM.check_persons==undefined){
                sweetAlertUtil.fail("检查指导人员必须填写")
                return
            }
            if($scope.startDates == ''){
                sweetAlertUtil.fail("检查指导时间")
                return
            }
            var params = {
                report_code: $scope.code, //报备编号
                start_date:$scope.startDate,//举办时间
                is_records:$scope.VM.is_records,//是否乡镇备案：Y(已备案)N（未备案）
                check_address:$scope.VM.check_address,//检查地点
                hold_person:$scope.VM.hold_person,//举办人
                is_aloof_pollute:$scope.VM.is_aloof_pollute, //远离污染源：Y(是)N（否）
                undertake_person:$scope.VM.undertake_person, //承办人
                is_well_clean:$scope.VM.is_well_clean, //清洁良好：Y(是)N（否）
                is_special_manager:$scope.VM.is_special_manager, //专业的管理：Y(是)N（否）
                manager_name:$scope.VM.manager_name, //管理人名称
                manager_phone:$scope.VM.manager_phone, //管理者电话
                is_health_cooks:$scope.VM.is_health_cooks, //帮厨是否健康：Y(是)N（否）
                is_poisonous_things:$scope.VM.is_poisonous_things, //加工场所（厨房）是否存放有毒有害物质（农药、鼠药等：Y(是)N（否）
                is_fresh_foods:$scope.VM.is_fresh_foods, //食品及食品原料安全卫生状况：Y(是)N（否）
                is_enough:$scope.VM.is_enough, //是否有充足的餐具：Y(是)N（否）
                foods_condition:$scope.VM.foods_condition, //食品加工情况：Y(是)N（否）
                store_condition:$scope.VM.store_condition, //食品存放情况：Y(是)N（否）
                is_pull_right:$scope.VM.is_pull_right, //是否现场改正：Y(是)N（否）
                check_persons:$scope.VM.check_persons, //检查指导人员
                check_date:$scope.startDates, //检查时间
            }


            sweetAlertUtil.confirm("确认提交检查信息?", function(isconfirm) {
                if (isconfirm) {
                    ReportApi.getReportLocal(params).then(function(data){
                        if(data.code == 200){
                            sweetAlertUtil.success("保存成功！","确定",function(){
                                $state.go("app.report.list")
                            })   
                        }
                    }, function(){})
                }

            })
        }
    }
]);
